package com.chat.api.web.restctrl;


import com.chat.api.web.po.UserBean;
import com.chat.api.web.service.UserService;
import com.chat.api.utils.ResultUtil;
import com.chat.api.web.vo.MessageEntity;
import com.chat.api.web.vo.UserEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/api/user/")
@RestController
public class UserCtrl {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation("获取主页面数据")
    public ModelAndView getAll(@RequestBody UserEntity entity, HttpServletRequest req){
        List<UserBean> all = userService.getAll();
        ModelAndView view = new ModelAndView();
        view.setViewName("main");
        view.addObject("data",all);
        return view;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation("用户注册")
    public MessageEntity getRegister(@RequestBody UserEntity entity, HttpServletRequest req){
        return userService.registerAccount(entity);
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation("用户登录")
    public MessageEntity getLogin(@RequestParam(name = "username") String username,
                                  @RequestParam(name = "password") String password,
                                  @RequestParam(name = "position") String position,
                                  HttpServletRequest req,HttpServletResponse response){
        return userService.login(username,password,req,response,position);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ApiOperation("用户登出")
    public MessageEntity getLogout( HttpServletRequest req,HttpServletResponse response) throws IOException {
            return userService.logOut(req,response);
    }


    @RequestMapping(value = "/sendCode", method = RequestMethod.POST)
    @ApiOperation("发送验证码")
    public MessageEntity sendCode(@RequestParam(value = "phone") String phone, HttpServletRequest req){
        return userService.sendCode(phone);
    }

    @RequestMapping(value = "/findPwd", method = RequestMethod.POST)
    @ApiOperation("找回密码")
    public MessageEntity getFindPwd(@RequestBody UserEntity entity, HttpServletRequest req){
        return userService.findPwd(entity);
    }

    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ApiOperation("皇家社区密码验证")
    public MessageEntity verify(@RequestBody UserEntity entity, HttpServletRequest req){
        Boolean result = userService.verify(entity);
        if (result){
            return ResultUtil.success("/royal/main.html");
        }else {
            return ResultUtil.fail("密码错误，请重新输入");
        }
    }

    @RequestMapping(value = "/adytum", method = RequestMethod.POST)
    @ApiOperation("密室密码验证")
    public MessageEntity adytum(@RequestBody UserEntity entity, HttpServletRequest req){
        Boolean result = userService.adytum(entity);
        if (result){
            return ResultUtil.success("/royal/main.html");
        }else {
            return ResultUtil.fail("密码错误，请重新输入");
        }
    }

    @RequestMapping(value = "/findUser", method = RequestMethod.GET)
    @ApiOperation("获取用户列表")
    public List<UserEntity> findUser(HttpServletRequest req){
        return userService.findUser(req);
    }

    @RequestMapping(value = "/findUserInfo", method = RequestMethod.GET)
    @ApiOperation("获取用户信息")
    public UserBean findUserInfo(HttpServletRequest req){
        return userService.findByUserInfo(req);
    }

}
